package com.qf.xiaomi.service.impl;

import com.qf.xiaomi.dao.CartDao;
import com.qf.xiaomi.dao.GoodsDao;
import com.qf.xiaomi.dao.impl.CartDaoImpl;
import com.qf.xiaomi.dao.impl.GoodsDaoImpl;
import com.qf.xiaomi.pojo.Cart;
import com.qf.xiaomi.pojo.Goods;
import com.qf.xiaomi.service.CartService;

import java.util.List;

/**
 * @author 千锋教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
public class CartServiceImpl implements CartService {

    CartDao dao = new CartDaoImpl();

    GoodsDao goodsDao = new GoodsDaoImpl();

    @Override
    public Cart findCartByUIdAndGoodsId(int uid, int goodsId) {
        return dao.findCartByUIdAndGoodsId(uid, goodsId);
    }

    @Override
    public void insert(Cart cart) {
        dao.insert(cart);
    }

    @Override
    public void update(Cart cart) {
        dao.update(cart);
    }

    @Override
    public List<Cart> getCarts(int uid) {
        //获取了该用户所有的购物车对象
        List<Cart> carts = dao.getCart(uid);
        //循环购物车， 对每一个购物车对象查询 对应的商品对象
        if(carts.size() > 0) {
            for (Cart cart : carts) {
                //查询每个购物车对应的商品对象
                int pid = cart.getPid();
                //根据商品id查询商品对象
                Goods goods = goodsDao.findById(pid);
                cart.setGoods(goods);
            }
        }

        return carts;
    }

    @Override
    public void deleteCart(int goodsId, int uid) {
        dao.deleteCart(uid, goodsId);
    }

    @Override
    public void clearCart(int uid) {
        dao.clearCart(uid);
    }


}
